﻿using System.Reflection;

namespace FastCode.Helper
{
    public class GenerageCodeHelper
    {
        public static void SetColumnConfig(PropertyInfo property, List<ColumnConfig> columnNames)
        {
            var controlTypeAttribute = property.GetCustomAttribute<UITypeAttribute>();
            var colItem = new ColumnConfig
            {
                Name = property.Name,
                Order = 100,
                Label = property.Name,
                ControlType = EUIType.Text,
                IsShow = true,
                IsEditable = true,
                TypeName = property.PropertyType.Name
            };



            colItem.IsNullable = ReflectionHelper.IsNullable(property.PropertyType);
            if (controlTypeAttribute != null)
            {
                var attColName = controlTypeAttribute.ColumnConfig;
                colItem.ControlType = attColName.ControlType;
                if (!string.IsNullOrEmpty(attColName.Label))
                {
                    colItem.Label = attColName.Label;
                }
                colItem.Order = attColName.Order;
                colItem.IsShow = attColName.IsShow;
                colItem.IsEditable = attColName.IsEditable;
            }
            //如果是枚举
            if (property.PropertyType.IsEnum)
            {
                colItem.ControlType = EUIType.Enum;
                colItem.EnumDic = EnumToDictionary(property.PropertyType);
            }
            if (colItem.IsShow)
            {

                columnNames.Add(colItem);
            }
        }


        static Dictionary<int, string> EnumToDictionary(Type type)
        {
            var enumDict = new Dictionary<int, string>();
            foreach (var value in Enum.GetValues(type))
            {
                enumDict.Add((int)value, value.ToString());
            }
            return enumDict;
        }
    }
}
